/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package ZooManagement;

/**
 * Zoo Class - Manages a collection of animals
 * @author ashongtical
 */
public class Zoo {
    public String Name;
    public Animal[] Animals;
    public int counter;

    public Zoo(String name) {
        this.Name = name;
        this.Animals = new Animal[10];
        this.counter = 0;
    }
    public void setName(String name) {
        Name = name;
    }

    public String getName() {
        return Name;
    }

    public void setAnimals(Animal[] Animals) {
        this.Animals = Animals;
    }

    public Animal[] getAnimals() {
        return Animals;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public int getCounter() {
        return counter;
    }

    //Use Boolean functions for feedback. If there is an empty position, it will return true.
    public boolean addAnimal(Animal animal) {
        if (counter >= Animals.length) {
            return false;
        }

        for (int i = 0; i < counter; i++) {
            Animal existingAnimal = Animals[i];
            if (existingAnimal.getName().equals(animal.getName())) {
                throw new IllegalArgumentException("Animal name already exists: " + animal.getName());
            }
        }

        Animals[counter] = animal;
        counter++;
        return true;
    }

    //Use Boolean functions for feedback. If an animal is found, it returns true.
    public boolean deleteAnimal(String animalName) {
        boolean deleted = false;

        // From end to start, find the animal to be deleted
        for (int i = counter - 1; i >= 0; i--) {
            if (Animals[i].getName().equals(animalName)) {
                for (int j = i; j < counter - 1; j++) {
                    Animals[j] = Animals[j + 1];
                }
                counter--;
                deleted = true;
            }
        }
        return deleted;
    }

    public int findAnimal(String animalName) {
        for(int i = 0; i < counter; i++) {
            if (Animals[i].getName().equals(animalName)) {
                return i;
            }
        }
        return -1;
    }

    public void moveAnimal(String animalName, Zoo targetZoo, Logistics logistics) {
        int index = findAnimal(animalName);
        if (index != -1) {
            Animal animal = Animals[index];
            if (targetZoo.addAnimal(animal)) {
                deleteAnimal(animalName);
                System.out.println("Animal " + animalName + " has been moved from " + this.Name + " to " + targetZoo.getName() + ".");
                System.out.println("Logistics details:");
                logistics.display();

            }
        } else {
            System.out.println("Animal " + animalName + " not found in " + this.Name + ".");
        }
    }

    // New method for printing the transfer receipt of animals
    public double getLogisticsPrice(Logistics logistics) {
        return logistics.getVehicle().getPrice() + logistics.getFuel().getPrice();
    }

    // Method to get an animal by index
    public Animal getAnimal(int index) {
        if (index >= 0 && index < counter) {
            return Animals[index];
        }
        return null;
    }
}


